package testGenerator.antlr.branchParser;

import testGenerator.antlr.testBuilders.TestBuilderInterface;
import testGenerator.antlr.testBuilders.BranchBuilderImp;
import gui.GUIController;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import testGenerator.Controller;

/**
 *
 * @author William Whitney
 */
public class BranchBuilder implements TestBuilderInterface
{

    private final Controller control;
    private final GUIController guiControl;
    private final BranchBuilderImp branchBuilderImp;
  

    public BranchBuilder(GUIController guiControl, Controller control)
    {
        this.control = control;
        this.guiControl = guiControl;
        branchBuilderImp = new BranchBuilderImp(guiControl, control);
     
    }

    /**
     * Examines the source code and captures the testing branches of interest.
     * @param code to be examined.
     */
    public void interpretSource()
    {
        String code = control.getSourceCode();
        try
        {
            //TODO fix depricated method
            InputStream iStream = new StringBufferInputStream(code);
            ANTLRInputStream input = new ANTLRInputStream(iStream);
            JavaBranchLexer lexer = new JavaBranchLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaBranchParser jParser = new JavaBranchParser(tokens, this);
            jParser.compilationUnit();

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void setError(String message)
    {
        guiControl.showLanguageErrorMessage(message);
    }

    public void setClassName(String className)
    {
        this.control.setClassName(className);
    }

    @Override
    public void reset()
    {
         branchBuilderImp.reset();
    }


    @Override
    public void addParam(String type, String id)
    {
        branchBuilderImp.addParam(type, id);
    }

    @Override
    public void addDoWhileSlot(int level)
    {
        branchBuilderImp.addDoWhileSlot(level);

    }

    @Override
    public void addDoWhileCondition(String doWhileCond, int level)
    {
        branchBuilderImp.addDoWhileCondition(doWhileCond, level);
    }

    @Override
    public void addSwitchCond(String cond, int level)
    {
        branchBuilderImp.addSwitchCond(cond, level);
    }

    @Override
    public void addIfConditionAtLevel(String ifCond, int level)
    {
        branchBuilderImp.addIfConditionAtLevel(ifCond, level);
    }

    @Override
    public void addElseConditionAtLevel(String elseCond, int level)
    {
        branchBuilderImp.addElseConditionAtLevel(elseCond, level);
    }

    @Override
    public void addWhileConditionAtLevel(String loopCond, int level)
    {
        branchBuilderImp.addWhileConditionAtLevel(loopCond, level);
    }

    @Override
    public void addForConditionAtLevel(String loopCond, int level)
    {
        branchBuilderImp.addForConditionAtLevel(loopCond, level);
    }

    @Override
    public void saveMethod(String returnType, String name)
    {
        branchBuilderImp.saveMethod(returnType, name);
    }

    @Override
    public void addModifier(String string)
    {
        branchBuilderImp.addModifier(string);
    }
}
